﻿
// 修复选中菜单
function _sitemap(module_id) {
    $('.sidebar-menu li').removeClass('active');
    var module_node = $('.sidebar-menu li[module-id="' + module_id + '"]');
    $(module_node).parent().parent().addClass('active');
    $(module_node).addClass('active');
}

var allDialog = [];
closeDialog = function () {
    if (allDialog.length > 0) {
        allDialog[allDialog.length - 1].close();
        allDialog.remove(allDialog[allDialog.length - 1]);
    }
}

reloadDialog = function () {
    var childWindow = document.getElementsByTagName("iframe");
    if (childWindow.length > 0) {
        for (var i in childWindow) {
            if (childWindow[i].contentWindow) {
                console.log(childWindow[i])
                childWindow[i].contentWindow.location.reload();
            }
        }
    }
}

var maxWidth = 750;
var maxHeight = 450;

function openDialog(url, title, size) {
    url = url + (url.indexOf('?') > -1 ? "&" : "?") + "dialogHeight=" + (size && size.height ? size.height : maxHeight);
    var currentDialog = dialog({
        width: size && size.width ? size.width : maxWidth,
        height: size && size.height ? size.height : maxHeight,
        fixed: true,
        content: '<iframe src="' + url + '" frameborder="0" style="width:100%; height:100%;"></iframe>',
        title: title,
    }).showModal();
    allDialog.push(currentDialog);
    return currentDialog;
}

function showDialogConfirm(options) {
    dialog(options).showModal();
}

function show_message(message, type) {
    var n = noty({
        text: message,
        layout: 'topCenter',
        type: type ? type : "success",
        timeout: 2000,
        dismissQueue: true,
    });
}

function showMask(title) {
    var html = '<div id="maskDiv" style="position: absolute; width: 100%; height: 100%; left: 0; top: 0; background:rgba(0,0,0,0.7); z-index: 10000; text-align:center;color:#fff; font-weight:bold; line-height: 40px;">';
    html += '       <img src="/content/images/loading.gif" style="margin-top:400px;    height: 80px;" />';
    html += '       <br />' + (title ? title : "");
    html += '   </div>';
    $('body').append(html);
}
function hideMask() {
    $('#maskDiv').remove();
}

function getQueryString(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
    var r = window.location.search.substr(1).match(reg);
    if (r != null) return unescape(r[2]); return null;
}

Date.prototype.format = function (fmt) {
    var o = {
        "M+": this.getMonth() + 1,
        "d+": this.getDate(),
        "h+": this.getHours(),
        "m+": this.getMinutes(),
        "s+": this.getSeconds(),
        "q+": Math.floor((this.getMonth() + 3) / 3),
        "S": this.getMilliseconds()
    };
    if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o)
        if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
    return fmt;
}

String.prototype.format = function (fmt) {
    var date = this.split("T");
    return (new Date(date[0] + " " + date[1].split(".")[0])).format(fmt);

}

Array.prototype.foreach = function (fn) {
    var data = this;
    if (data) {
        for (var i = 0; i < data.length; i++) {
            fn(data[i]);
        }
    }
}
Array.prototype.where = function (fn) {
    var result = [];
    if (this) {
        this.foreach(function (item) {
            if (fn(item)) {
                result.push(item);
            }
        });
    }
    return result;
}
Array.prototype.getItem = function (fn) {
    var result;
    if (this) {
        this.foreach(function (item) {
            if (fn(item)) {
                result = item;
            }
        });
    }
    return result;
}
Array.prototype.indexOf = function (val) {
    for (var i = 0; i < this.length; i++) {
        if (this[i] == val) return i;
    }
    return -1;
};
Array.prototype.remove = function (val) {
    var index = this.indexOf(val);
    if (index > -1) {
        this.splice(index, 1);
    }
};

String.prototype.replaceAll = function (s1, s2) {
    return this.replace(new RegExp(s1, "gm"), s2);
}


//封装数据对象，增加防CSRF字段，服务端应相应增加Action属性
function AntiCSRF(data) {
    var token = $("input[name='__RequestVerificationToken']").val();
    if (data) {
        data["__RequestVerificationToken"] = token;
        return data;
    }
    else {
        return { __RequestVerificationToken: token };
    }
}

//封装通用确认对话框，传入标题、确认操作函数、取消操作函数
function showConfirm(title, okFunc, cancelFunc) {
    dialog({
        title: "确认操作",
        content: title,
        width: "280",
        okValue: "确认",
        ok: okFunc,
        cancelValue: "取消",
        cancel: cancelFunc,
        zIndex: 10000
    }).showModal();
}

function imageError(sender) {
    $(sender).attr("src", "/Content/images/404.jpg");
}

//验证手机格式
function isIpone(ipone) {
    var reg = /^((13[0-9])|(14[0-9])|(15[0-9])|(17[0-9])|(18[0-9]))[0-9]{8}$/;
    if (ipone != "") {
        if (reg.test(ipone) === false) {
            show_message("请输入正确格式的手机号", "warning");
            return false;
        }
    } else {
        show_message("请输入手机号", "warning");
        return false;
    }
    return true;
}

//验证码倒计时
var countdown = 30;
function settime(obj) {
    if (countdown == 0) {
        obj.attr("disabled", false);
        countdown = 30;
        return;
    } else {
        obj.attr("disabled", true).attr('style', 'background:#ccc;');
        obj.val("重新发送 (" + countdown + ")");
        countdown--;
    }
    setTimeout(function () {
        settime(obj);
        if (countdown <= 0) {
            obj.attr('style', 'background:#1F8AE7;');
            obj.val("获取验证码");
            obj.attr("disabled", false);
            clearTimeout();
        }
    }, 1000)
}

$(function () {
    $('.areatext').each(function () {
        var id = $(this).attr('data-id');
        var result = getCityById(id);
        if (result.province) {
            $(this).html(result.province + "" + result.city + "" + result.county);
        }
    });
});